package com.story.api.moduler.user.validator.impl;

import com.story.api.common.persistence.model.AppUser;
import com.story.api.common.persistence.model.UserLogin;
import com.story.api.moduler.user.constant.LoginType;
import com.story.api.moduler.user.dao.AppUserDao;
import com.story.api.moduler.user.dao.UserLoginDao;
import com.story.api.moduler.user.validator.IReqValidator;
import com.story.api.moduler.user.validator.dto.Credence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Administrator
 * @decription
 * @Date 2018-01-03 15:07
 */
@Service
public class SocialValidator implements IReqValidator{

    @Autowired
    private UserLoginDao userLoginDao;

    @Autowired
    private AppUserDao userDao;

    @Override
    public boolean validate(Credence credence) {
        //查询，如果没有微信，qq信息，则将账号进行注册
        int count = userLoginDao.userLogin(credence.getCredenceName(),credence.getCredenceCode(),credence.getCredenceType());
        if(count > 0) {
            return true;
        }
        if(LoginType.PHONE_TYPE.equals(credence.getCredenceType())) {
            return false;
        }
        //先构建用户信息
        AppUser user = new AppUser();
        user.setRegisterDate(new Date());
        userDao.saveUser(user);
        //在构建账号信息
        UserLogin userLogin = new UserLogin();
        userLogin.setIdentifier(credence.getCredenceName());
        userLogin.setCredential(credence.getCredenceCode());
        userLogin.setIdentityType(credence.getCredenceType());
        userLogin.setUserId(user.getId());
        userLoginDao.saveUserLogin(userLogin);
        //最后返回成功信息
        return true;
    }
}
